package com.ebb;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.stackmob.android.sdk.common.StackMobCommon;
import com.stackmob.sdk.callback.StackMobCallback;
import com.stackmob.sdk.exception.StackMobException;
import com.stackmob.sdk.push.StackMobPushToken;

public class EBBMessageActivity extends Activity implements OnClickListener {
	private EditText etMessage;
	private Button btnSend;
	private Button btnCancel; 
	private static final String TAG = EBBStackMobActivity.class.getCanonicalName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message);
		
        EBBAlert.setContext(EBBMessageActivity.this);
        EBBAlert.setDuration(Toast.LENGTH_SHORT);

		EBBC2DMRegistrationIDHolder regHolder = new EBBC2DMRegistrationIDHolder(this);
		if (regHolder.hasID()) {
			try {
				Log.i(TAG, "registration ID " + regHolder.getID() + " was already stored in shared prefs");
				EBBAlert.message(1, null, "registration ID " + regHolder.getID() + " was already stored in shared prefs");
			}
			catch (EBBC2DMRegistrationIDHolder.NoStoredRegistrationIDException e) {
				Log.e(TAG, "failed to get registration ID from shared prefs, even though shared prefs reports that it's there" , e);
				EBBAlert.message(1, null, "failed to get registration ID from shared prefs, even though shared prefs reports that it's there");
			}
		} else {
			Log.i(TAG, "registration ID was not already stored in shared prefs. fetching a new one and saving it");
			EBBAlert.message(1, null, "registration ID was not already stored in shared prefs. fetching a new one and saving it");
			try {
				requestRegistrationId();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    	
		initLayout();
	}
	
	private void initLayout() {
		this.etMessage = (EditText) findViewById(R.id.msg_value);
		
		this.btnSend = (Button) findViewById(R.id.send_button);
		this.btnCancel = (Button) findViewById(R.id.cancel_button);

		this.btnSend.setId(0x5001);
		this.btnCancel.setId(0x5002);

		this.btnSend.setOnClickListener(this);
		this.btnCancel.setOnClickListener(this);
	}
	
	public void requestRegistrationId() throws Exception {
		try {
			EBBAlert.message(1, null, getRegistrationIDHolder().getID());
		}
		catch(EBBC2DMRegistrationIDHolder.NoStoredRegistrationIDException e) {
			Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
			intent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
			intent.putExtra("sender", "apexcns.test@gmail.com");
			startService(intent);
		}
	}
	
	private EBBC2DMRegistrationIDHolder getRegistrationIDHolder() {
		return new EBBC2DMRegistrationIDHolder(EBBMessageActivity.this);
	}

	public void sender() throws EBBC2DMRegistrationIDHolder.NoStoredRegistrationIDException {
//			StackMobCommon.getStackMobInstance().registerForPushWithUser("test", getRegistrationIDHolder().getID(), new StackMobCallback() {
//				@Override
//				public void success(String s) {
//					runOnUiThread(new Runnable() {
//						@Override
//						public void run() {
//							EBBAlert.message(1, null, "Set User Success!");
//						}
//					});
//				}
//				
//				@Override
//				public void failure(StackMobException e) {
//					runOnUiThread(new Runnable() {
//						@Override
//						public void run() {
//							EBBAlert.message(1, null, "Set User Failure!");
//						}
//					});
//					e.printStackTrace();
//				}
//			});
			final Map<String, String> payload = new HashMap<String, String>();
			payload.put("payload", etMessage.getText().toString());
			EBBAlert.message(1, null, "payload: " + payload.get("payload"));
			StackMobPushToken token = new StackMobPushToken(getRegistrationIDHolder().getID(), StackMobPushToken.TokenType.Android);
			StackMobCommon.getStackMobInstance().pushToTokens(payload, Arrays.asList(token), new StackMobCallback() {
//			StackMobCommon.getStackMobInstance().broadcastPushNotification(payload, new StackMobCallback() {
				@Override
				public void success(String s) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							EBBAlert.message(1, null, "Send Success!");
						}
					});
				}
				
				@Override
				public void failure(StackMobException e) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							EBBAlert.message(1, null, "Send Failure!");
						}
					});
					e.printStackTrace();
				}
			});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case 0x5001:
			// �޼����� ������.
			try {
				sender();
				Log.v(TAG, "Send Message: " + etMessage.getText().toString());
//				EBBAlert.message(1, null, "Send Message: " + etMessage.getText().toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case 0x5002:
			finish();
			break;
		}
	}
}